﻿<?php
@session_start();
include_once("../initial.php");
$DOCUMENT_ROOT=$_SERVER["DOCUMENT_ROOT"];
include_once(constant('PROJECT_DIR')."/logic/userLogic.php");
include_once(constant('PROJECT_DIR')."/logic/messageLogic.php");
require_once("../checkLogin.php");
$LIST_NUMBER_PER_PAGE=20;

if($login==true)
{
		$smarty->assign("userId", $uid);
		$smarty->assign("unReadNum", $unReadNum);
}
//begin to read class
$uid = $_SESSION['user'];
$ml = new messageLogic();
$ul = new userLogic();
$action="";
if(isset($_POST['action']))
{
     $action = $_POST['action'];
}
else if(isset($_GET['action']))
{
     $action=$_GET['action'];
}
$note="";

//验证用户登录


$ml = new messageLogic();

$action = @$_GET['action'];
$view = @$_GET['view'];
if($view =="")
{
    $view = "recieve";
}
if($action=="")
{
    $action="view";
}

$smarty->assign("action", $action);

switch($action)
{
    case "open":
        $messageid = $_GET['messageid'];
        if($messageid=="")
        {
            break;
        }
        $currentMessage = $ml->get_message(array('id'=>$messageid),"","");
        if(is_array($currentMessage)==false)
        {
            break;
        }
        if($currentMessage[0]['recieveuid']==$uid)
        {
            $ml->update_message(array("read"=>"1"),$messageid);
        }
        //expend user
        $senderuserinfo="";
        $senderuserinfo = $ul->get_user(array('id'=>$currentMessage[0]['senduid']),"","");
        $currentMessage[0]['senduser']=$senderuserinfo[0]['id'];
        $currentMessage[0]['sendname']=$senderuserinfo[0]['name'];
        $currentMessage[0]['senduid']=$senderuserinfo[0]['id'];

        $smarty->assign("currentMessage",$currentMessage[0]);

        break;

    case "reply":
        //$recieveuserinfo[userid,usertype,id]
        $recieveid = $_GET['recieveid'];
        $recieveusertype = $_GET['recieveusertype'];
        if($recieveid=="")
        {
            break;
        }
        $recieveuserinfo="";
        $recieveuserinfo = $ul->get_user(array('id'=>$recieveid),"","");
        $smarty->assign("recieveuserinfo",$recieveuserinfo[0]);

        //--------------get the old message
        $messageid = $_GET['messageid'];
        if($messageid=="")
        {
            break;
        }
        $currentMessage = $ml->get_message(array('id'=>$messageid),"","");

        if(is_array($currentMessage)==false)
        {
            break;
        }
        $ml->update_message(array("read"=>"1"),$messageid);
        print_r($currentMessage);
        $smarty->assign("currentMessage",$currentMessage[0]);
        //-------------------get the old message end
        break;
    case "send":
        $title=$_POST['title'];
        $content=$_POST['content'];
        if($title=="" || $content=="")
        {
            $_SESSION['notice']="标题和内容不能为空";
            break;
        }
        $recieveid=$_POST['recieveid'];
        if($recieveid=="")
        {
            $recieveuser = $_POST['userid'];
                $recieveusertype="0";
                $recieveuserinfo = $ul->get_user(array('id'=>$recieveuser),"","");
            if($recieveuserinfo[0]['id']=="")
            {
                $_SESSION['notice']="收件人不存在，发送失败";
                break;
            }
            $recieveid=$recieveuserinfo[0]['id'];
        }

        $messagearray = array();
        $title = substr($title,0,300);
        $content = substr($content,0,10000);

        $title=str_replace("\'","\\'",$title);
        $title=str_replace('\"','\\"',$title);

        $content=str_replace("\'","\\'",$content);
        $content=str_replace('\"','\\"',$content);

        $messagearray['title']=$title;
        $messagearray['content']=$content;
        $messagearray['senduid']=$uid;
        $messagearray['recieveuid']=$recieveid;
        $messagearray['createtime']=date("Y-m-d H:i:s");

        $ml->insert_message($messagearray);
        $_SESSION['notice']="请查看已发件箱，确定是否已经发送成功";
        break;
    case "delete":
        $view = $_GET['view'];
        $messageid = $_GET['messageid'];
        if($view =="send")
        {
            $temp = $ml->get_message(array("id"=>$messageid,"senduid"=>$uid),"","");

            if(count($temp)==1)
            {
                $ml->delete_send_message($messageid);
            }
        }
        else if($view == "recieve")
        {
            $temp = $ml->get_message(array("id"=>$messageid,"recieveuid"=>$uid),"","");
            if(count($temp)==1)
            {
                $ml->delete_recieve_message($messageid);
            }
        }
        else
        {
            $_SESSION['notice']="非法操作";
        }
        break;
    default:
        break;
}

switch($view)
{
    case "send":
        $messagelist = $ml->get_message(array('senduid'=>$uid,'sdelete'=>"0"),"order by createtime desc","");
        $messagenotreadNumber = $ml->get_unread_message_num($uid);
        $smarty->assign("newMessageNumber",$messagenotreadNumber);

        //expend the user information
        if(count($messagelist)<=0)
        {
            break;
        }
        if(is_array($messagelist)==false)
        {
            $messagelist=array();
        }
        foreach($messagelist as &$m)
        {

                $temp= $ul->get_user(array("id"=>$m['senduid']),"","");
                $m['sendname']=$temp[0]['name'];
                $m['senduser']=$temp[0]['id'];
        }
        $smarty->assign("messagelist",$messagelist);
        break;
    case "recieve";
        $messagelist = $ml->get_message(array('recieveuid'=>$uid,'rdelete'=>"0"),"order by createtime desc","");
        $messagenotreadNumber = $ml->get_message_num(array('recieveuid'=>$uid, "read"=>"0",'rdelete'=>"0"));
        print($messagenotreadNumber);
        $smarty->assign("newMessageNumber",$messagenotreadNumber);

        //expend the user information
        if(count($messagelist)<=0)
        {
            break;
        }
        if(is_array($messagelist)==false)
        {
            $messagelist=array();
        }
        foreach($messagelist as &$m)
        {
                $temp= $ul->get_user(array("id"=>$m['senduid']),"","");
                print_r($temp);
                $m['sendname']=$temp[0]['name'];
                $m['senduser']=$temp[0]['id'];
        }
        $smarty->assign("messagelist",$messagelist);
        break;
    default:
        break;
}


$smarty->assign("action",$action);
$smarty->assign("view",$view);
if(isset($_SESSION['notice'])){
     $smarty->assign("notice",$_SESSION['notice']);
}
$_SESSION['notice']="";

$smarty->display("message/message.html");
?>


